package com.cea.papyrus.core.actions.selectcopypaste;

import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;

import com.cea.papyrus.core.commands.copypaste.PasteFromClipboardCommand;
import com.cea.papyrus.core.treeeditpart.ElementTreeEditPart;

/**
 * An action to paste from clipboard in the selected element.
 */
public class PasteAction extends SelectionAction {

	/** standard ID of the action in the workbench */
	public static final String ID = ActionFactory.PASTE.getId();

	/**
	 * @param editor The editor this action will be associated with.
	 */
	public PasteAction(IEditorPart editor) {
		this((IWorkbenchPart)editor);
	}

	/**
	 * Creates a <code>CopyAction</code> with the given label.
	 * @param editor The editor this action will be associated with.
	 * @param label  The label to be displayed for this action.
	 */
	public PasteAction(IEditorPart editor, String label) {
		this((IWorkbenchPart)editor);
		setText(label);
	}

	/**
	 * Constructs a <code>CopyAction</code> using the specified part.
	 * @param part The part for this action
	 */
	public PasteAction(IWorkbenchPart part) {
		super(part);
		setLazyEnablementCalculation(false);
	}

	/**
	 * Returns <code>true</code> if the selected objects can
	 * be deleted.  Returns <code>false</code> if there are
	 * no objects selected or the selected objects are not
	 * {@link EditPart}s.
	 * @return <code>true</code> if the command should be enabled
	 */
	protected boolean calculateEnabled() {
		Command cmd = createPasteCommand(getSelectedObjects());
		if (cmd == null)
			return false;
		return cmd.canExecute();
	}

	/**
	 * Create a command to copy the selected objects.
	 * @param objects The objects to be copied.
	 * @return The command to copy the selected objects.
	 */
	public Command createPasteCommand(List objects) {
		// try to get the editing domain for emf.
		// if no editing domain is available, return an unexecutable command
		EditingDomain domain = (EditingDomain)getWorkbenchPart().getAdapter(EditingDomain.class);
		if(domain == null) {
			return UnexecutableCommand.INSTANCE;
		}
		
		CompoundCommand command = new CompoundCommand();
		// for each element selected, creates a pasteFromClipboardCommand
		Iterator it= getSelectedObjects().iterator();
		while(it.hasNext()) {
			// current selected element
			Object o = it.next();
			if(o instanceof ElementTreeEditPart) {
				command.add( new PasteFromClipboardCommand(((ElementTreeEditPart)o).getUmlElement(), domain));
			}
		}
		return command;	
	}

	/**
	 * Initializes this action's text and images.
	 */
	protected void init() {
		super.init();
		setText("Paste");
		setToolTipText("Paste from the clipboard");
		setId(ID);
		ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
		setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_PASTE));
		setDisabledImageDescriptor(sharedImages.getImageDescriptor(
				ISharedImages.IMG_TOOL_PASTE_DISABLED));
		setEnabled(false);
	}

	/**
	 * Performs the delete action on the selected objects.
	 */
	public void run() {
		execute(createPasteCommand(getSelectedObjects()));
	}

}
